package cn.buk.qms.web;

import cn.buk.common.JsonResult;
import cn.buk.common.dto.CommonDto;
import cn.buk.common.sc.CommonSearchCriteria;
import cn.buk.tms.dto.PnrSummary;
import cn.buk.tms.entity.misc.Pnr;
import cn.buk.tms.entity.misc.PnrLog;
import cn.buk.tms.entity.misc.PnrMobile;
import cn.buk.tms.entity.misc.PnrUsed;
import cn.buk.qms.service.PnrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *
 * @author yfdai
 * @date 2017/1/10
 */
@RestController
@RequestMapping("/pnr")
public class PnrController extends BaseController {

    @Autowired
    private PnrService pnrService;


    @Override
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));//true:允许输入空值，false:不能为空值
    }

    @Override
    @InitBinder("sc")
    public void initBinder1(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("sc.");
    }


    /**
     * 查找PNR
     * @param request
     * @param sc
     * @return
     */
    @RequestMapping("/list")
    public CommonDto<Pnr> searchPnr(HttpServletRequest request,
                                    @ModelAttribute("sc") CommonSearchCriteria sc) {
        final int enterpriseId = getEnterpriseId(request);

        List<Pnr> pnrs = pnrService.searchPnr(enterpriseId, sc);

        CommonDto<Pnr> pnrCommonDto = new CommonDto<>();
        pnrCommonDto.setDataList(pnrs);
        pnrCommonDto.setPage(sc.getPage());

        return pnrCommonDto;
    }

    @RequestMapping("/listUsed")
    public CommonDto<PnrUsed> searchPnrUsed(HttpServletRequest request,
                                    @ModelAttribute("sc") CommonSearchCriteria sc) {
        final int enterpriseId = getEnterpriseId(request);

        List<PnrUsed> pnrs = pnrService.searchPnrUsed(sc, enterpriseId);

        CommonDto<PnrUsed> pnrCommonDto = new CommonDto<>();
        pnrCommonDto.setDataList(pnrs);
        pnrCommonDto.setPage(sc.getPage());

        return pnrCommonDto;
    }

    @RequestMapping("/used/analyse")
    public JsonResult searchPnrUsed(HttpServletRequest request) {
        final int enterpriseId = getEnterpriseId(request);

        return pnrService.analyzeUsedPnr(enterpriseId);
    }

    @RequestMapping("/{id}")
    public Pnr getPnrDetail(HttpServletRequest request, @PathVariable("id") final int id) {
        final int enterpriseId = getEnterpriseId(request);

        return pnrService.getPnrById(id, enterpriseId);
    }

    /**
     * 监控PNR设置
     */
    @RequestMapping("/monitor")
    public JsonResult savePnrMonitor(HttpServletRequest request,
                                     @RequestParam("pnr") String pnr,
                                     @RequestParam("monitorType") int monitorType) {
        final int enterpriseId = getEnterpriseId(request);

        return pnrService.savePnrMonitor(enterpriseId, pnr, monitorType);
    }


    @RequestMapping("/stat")
    public CommonDto<PnrSummary> statPnrByUsername(HttpServletRequest request,
                                                   @RequestParam(value="beginDate") @DateTimeFormat(pattern="yyyy-MM-dd") final Date beginDate,
                                                   @RequestParam(value="endDate") @DateTimeFormat(pattern="yyyy-MM-dd") final Date endDate,
                                                   @RequestParam(value = "etermUsername", defaultValue = "") final String etermUsername) {
        final int enterpriseId = getEnterpriseId(request);

        CommonSearchCriteria sc = new CommonSearchCriteria();
        sc.setBeginDate(beginDate);
        sc.setEndDate(endDate);
        sc.setEtermUsername(etermUsername);

        List<PnrSummary> stats = pnrService.statPnrByUsername(enterpriseId, sc);

        CommonDto<PnrSummary> commonDto = new CommonDto<>();
        commonDto.setDataList(stats);
        commonDto.setPage(sc.getPage());

        return commonDto;
    }

    /**
     * 缺少 CTCM 的 PNR
     * @param request
     * @param carrier
     * @param etermUsername
     * @param sc
     * @return
     */
    @RequestMapping("/ctcm")
    public CommonDto<Pnr> searchPnrCtcm(HttpServletRequest request,
                                        @RequestParam(value = "carrier", defaultValue = "") String carrier,
                                        @RequestParam(value = "etermUsername", defaultValue = "") String etermUsername,
                                        @ModelAttribute("sc") CommonSearchCriteria sc) {
        final int enterpriseId = getEnterpriseId(request);

        List<Pnr> pnrs = pnrService.searchPnrCtcm(enterpriseId, carrier, etermUsername, sc);

        CommonDto<Pnr> dto = new CommonDto<>();
        dto.setDataList(pnrs);
        dto.setPage(sc.getPage());

        return dto;
    }

    @RequestMapping("/ctct")
    public CommonDto<Pnr> searchPnrCtcm(HttpServletRequest request,
                                        @ModelAttribute("sc") CommonSearchCriteria sc) {
        final int enterpriseId = getEnterpriseId(request);

        List<Pnr> pnrs = pnrService.searchPnrCtct(enterpriseId, sc);

        CommonDto<Pnr> dto = new CommonDto<>();
        dto.setDataList(pnrs);
        dto.setPage(sc.getPage());

        return dto;
    }

    @GetMapping(value = "/mobile")
    public CommonDto<PnrMobile> searchPnrMobile(HttpServletRequest request, @ModelAttribute("sc") CommonSearchCriteria sc) {
        final int enterpriseId = getEnterpriseId(request);

        List<PnrMobile> list = pnrService.searchPnrMobile(enterpriseId, sc);

        CommonDto<PnrMobile> dto = new CommonDto<>();
        dto.setDataList(list);
        dto.setPage(sc.getPage());

        return dto;
    }

    @PostMapping(value = "/mobile")
    public JsonResult searchPnrMobile(HttpServletRequest request,
                                      @RequestParam(value = "mobile", defaultValue = "") final String mobile) {
        final int enterpriseId = getEnterpriseId(request);

        return pnrService.createPnrMobile(enterpriseId, mobile);
    }

    @PostMapping(value = "/mobile/delete")
    public JsonResult deletePnrMobile(HttpServletRequest request,
                                      @RequestParam(value = "id") final int id) {
        final int enterpriseId = getEnterpriseId(request);

        return pnrService.deletePnrMobile(enterpriseId, id);
    }

    /**
     * 查找log
     * @param request
     * @param sc
     * @return
     */
    @RequestMapping("/syslog")
    public CommonDto<PnrLog> searchPnrLog(HttpServletRequest request,
                                          @ModelAttribute("sc") CommonSearchCriteria sc) {
        final int enterpriseId = getEnterpriseId(request);

        List<PnrLog> pnrs = pnrService.searchPnrLog(enterpriseId, sc);

        CommonDto<PnrLog> dto = new CommonDto<>();
        dto.setDataList(pnrs);
        dto.setPage(sc.getPage());

        return dto;
    }
}
